package cn.openea.perpartner.wxpay.wxpaysdk;

import java.io.*;
import org.springframework.beans.factory.annotation.Value;

/**
 * @Name: 微信WXPayConfig配置文件初始化参数
 * @author: Mr.Yun
 * @Created: 2022/10/13 20:50
 */
public class MyWxPayConfig extends WXPayConfig {

    @Value("${wxpay.appid}")
    private String appid;
    @Value("${wxpay.mchid}")
    private String mchid;
    @Value("${wxpay.mchkey}")
    private String mchkey;
    private byte[] certData;

    public MyWxPayConfig() throws IOException {
        // 获取证书地址的文件流，读取项目文件流参考：https://blog.csdn.net/xixiyuguang/article/details/122705049
        InputStream resourceAsStream = this.getClass().getResourceAsStream("/META-INF/cert/NyZSnqLRwTnr2s9C.p12");
        this.certData = new byte[resourceAsStream.available()];
        resourceAsStream.read(this.certData);
    }

    @Override
    public String getAppID() {
        return this.appid;
    }

    @Override
    public String getMchID() {
        return this.mchid;
    }

    @Override
    public String getKey() {
        return this.mchkey;
    }

    @Override
    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    @Override
    public IWXPayDomain getWXPayDomain() {
        // 这个方法需要这样实现, 否则无法正常初始化WXPay
        IWXPayDomain iwxPayDomain = new IWXPayDomain() {
            public void report(String domain, long elapsedTimeMillis, Exception ex) {
            }
            public DomainInfo getDomain(WXPayConfig config) {
                return new DomainInfo(WXPayConstants.DOMAIN_API, true);
            }
        };
        return iwxPayDomain;
    }

}
